﻿@using Dignite.Abp.Notifications
@using Microsoft.Extensions.Localization
@using Volo.Abp
@inherits NotificationDataComponentBase<LocalizableMessageNotificationData>
<Paragraph>
    @Localize()
</Paragraph>
@code{

    private LocalizedString Localize()
    {
        var localizer = CreateStringLocalizerOrNull(StringLocalizerFactory);
        if (localizer == null)
        {
            throw new AbpException($"Set {nameof(NotificationData.ResourceName)} or configure the default localization resource type (in the AbpLocalizationOptions)!");
        }

        var result = localizer[NotificationData.Name];

        if (result.ResourceNotFound && NotificationData.ResourceName != null)
        {
            /* Search in the default resource if not found in the provided resource */
            localizer = StringLocalizerFactory.CreateDefaultOrNull();
            if (localizer != null)
            {
                result = localizer[NotificationData.Name];
            }
        }

        return result;
    }


    private IStringLocalizer CreateStringLocalizerOrNull(IStringLocalizerFactory stringLocalizerFactory)
    {
        if (NotificationData.ResourceName != null)
        {
            var localizerByName = stringLocalizerFactory.CreateByResourceNameOrNull(NotificationData.ResourceName);
            if (localizerByName != null)
            {
                return localizerByName;
            }
        }

        return stringLocalizerFactory.CreateDefaultOrNull();
    }
}
